import { toast, setStorage, removeStorage } from "../../utils/storage"
Page({
  data: {
    unionid: '',
    bindCode: ''
  },
  onLoad({ unionid }) {
    this.setData({ unionid });
  },
  goback() {
    wx.navigateBack();
  },
  async bindUser() {
    if (this.data.bindCode.trim() === '') {
      toast({ title: "请输入验证码" });
      return;
    }
    let { statusCode, data: { code, data, message } = {} } = await wx.$http.post("/quality/wxBindAccount",
      {
        unionid: this.data.unionid,
        bindCode: this.data.bindCode
      }).catch(e => e);
    if (statusCode === 200 && code === 200) {
      setStorage(wx.$TOKEN_NAME, data);//绑定令牌
      //获取当前登录用户角
      ({ statusCode, data: { code, data, message } } = await wx.$http.get("/quality/mgr/userRole").catch());
      if (statusCode === 200 && code === 200) {
        let { sysRoleVOList: roles } = data;
        roles = roles.map(x => ({ roleCode: x.roleCode, roleName: x.roleName }));
        if (roles.filter(x => x.roleCode === "STUDENT").length === 0) {//查询是否存在学生角色
          toast({ title: "当前系统版本仅支持学生登录" });
          removeStorage(wx.$TOKEN_NAME);
          return;
        }
        setStorage(wx.$USER_NAME, data);//当前登录用户角色
        wx.redirectTo({
          url: '/pages/squra/squra',
        });
      }
    }
    else
      toast({ title: message })
  }
})